//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	SiteQueryLists.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Smart.Common.Entities
{
	/// <summary>
	/// Site data query lists filter
	/// </summary>
	[Localizable(false)]
	public class SiteQueryLists
	{
		/// <summary>
		/// Site data query
		/// </summary>
		public SiteQuery SiteQuery { get; set; }

		/// <summary>
		/// List template
		/// </summary>
		public string ServerTemplate { get; set; }

		/// <summary>
		/// List base type
		/// </summary>
		public string BaseType { get; set; }

		/// <summary>
		/// Flag whether to include hidden lists
		/// </summary>
		public bool IncludeHiddenLists { get; set; }

		/// <summary>
		/// Maximum total number of lists
		/// </summary>
		public int MaxListsLimit { get; set; }

		/// <summary>
		/// Indexed field name, title, guid
		/// </summary>
		public string WithIndexField { get; set; }

		/// <summary>
		/// Indexed field value
		/// </summary>
		public object WithIndexFieldValue { get; set; }

		/// <summary>
		/// Lists name, urls, guids
		/// </summary>
		public List<string> Lists { get; set; }

		/// <summary>
		/// Site data query lists filter
		/// </summary>
		public SiteQueryLists(SiteQuery siteQuery)
		{
			SiteQuery = siteQuery;

			IncludeHiddenLists = false;
			MaxListsLimit = 1000;

			Lists = new List<string>();
		}

		/// <summary>
		/// Format attributes
		/// </summary>
		[Localizable(false)]
		public virtual string FormatAttributes()
		{
			var res = string.Empty;

			if (!ServerTemplate.IsNullOrEmpty())
				res += " ServerTemplate=\"" + ServerTemplate.EncodeXml() + "\"";

			if (!BaseType.IsNullOrEmpty())
				res += " BaseType=\"" + BaseType.EncodeXml() + "\"";

			if (IncludeHiddenLists)
				res += " Hidden=\"TRUE\"";

			if (MaxListsLimit != 1000)
				res += " MaxListLimit=\"" + MaxListsLimit + "\"";

			return res;
		}

		/// <summary>
		/// Format lists query
		/// </summary>
		public virtual string Format()
		{
			string res;

			if (WithIndexField != null && WithIndexFieldValue != null)
			{
				string valueType;
				string value;

				if (WithIndexFieldValue is string)
				{
					valueType = "Text";
					value = (string)WithIndexFieldValue;
				}
				else if (WithIndexFieldValue is int)
				{
					valueType = "Integer";
					value = WithIndexFieldValue.ToString();
				}
				else if (WithIndexFieldValue is double)
				{
					valueType = "Number";
					value = ((double)WithIndexFieldValue).ToString(H.En.NumberFormat);
				}
				else if (WithIndexFieldValue is float)
				{
					valueType = "Number";
					value = ((float)WithIndexFieldValue).ToString(H.En.NumberFormat);
				}
				else
				{
					throw new NotSupportedException("WithIndexFieldValue type " + WithIndexFieldValue.GetType().Name);
				}

				res =
					string.Format(
						@"<Lists{0}><WithIndex FieldId=""{1:D}"" Type=""{2}"" Value=""{3}"" /></Lists>",
						FormatAttributes(),
						SiteQuery.Meta.FieldId(WithIndexField),
						valueType,
						value.EncodeXml());
			}
			else if (Lists.Count > 0)
			{
				var metaWeb = MetaCtx.Web;

				for (int i = 0, iCnt = Lists.Count; i < iCnt; i++)
				{
					var list = Lists[i];

					if (!list.IsGuid())
					{
						var listId = metaWeb.ListId(list);

						if (listId.IsEmpty())
						{
							Lists.RemoveAt(i);

							i--;
							iCnt--;
						}
						else
						{
							Lists[i] = listId.ToString("D");
						}
					}
					else
					{
						Lists[i] = list.SToGuid().ToString("D");
					}
				}

				res
					= string.Concat(
						"<Lists",
						FormatAttributes(),
						">",
						Lists.CastToString(string.Empty, s => string.Concat(@"<List ID=""", s, @""" />")),
						"</Lists>");
			}
			else
			{
				res
					= string.Concat(
						"<Lists",
						FormatAttributes(),
						"/>");

				if (res.EqIC("<Lists/>"))
					res = null;
			}

			return res;
		}
	}
}